package com.ny.controller;

import com.ny.entity.Devices;
import com.ny.entity.Result;
import com.ny.service.impl.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    // 查询所有设备
    @GetMapping("/findAllDevices")
    public Result findAllDevices() {
        return Result.success(deviceService.findAll());
    }

    // 添加设备
    @PostMapping("/addDevice")
    public Result addDevice(@RequestBody Devices device) {
        Devices savedDevice = deviceService.addDevice(device);
        return savedDevice != null ? Result.success(savedDevice) : Result.error("添加设备失败");
    }

    // 修改设备
    @PutMapping("/updateDevice/{deviceId}")
    public Result updateDevice(@PathVariable int deviceId, @RequestBody Devices device) {
        Devices updatedDevice = deviceService.updateDevice(deviceId, device);
        return updatedDevice != null ? Result.success(updatedDevice) : Result.notFound("设备不存在");
    }

    // 删除设备
    @DeleteMapping("/deleteDevice/{deviceId}")
    public Result deleteDevice(@PathVariable int deviceId) {
        boolean isDeleted = deviceService.deleteDevice(deviceId);
        return isDeleted ? Result.success("删除成功") : Result.notFound("设备不存在");
    }

    // 模糊查询设备
    @GetMapping("/searchDevices")
    public Result searchDevices(@RequestParam String keyword) {
        List<Devices> devices = deviceService.findByKeyword(keyword);
        return Result.success(devices);
    }
}